<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="query">
				<el-form-item>
					<el-input v-model="query.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getDictionarys">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="dictionarys" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="sn" label="标识" width="120" sortable>
			</el-table-column>
			<el-table-column prop="title" label="title" width="180" sortable>
			</el-table-column>
      <el-table-column prop="intro" label="简介" width="180" sortable>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="120" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.status == 1" style="color: #13ce66">正常</span>
          <span v-else style="color: red">禁用</span>
        </template>
      </el-table-column>

			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条 批量删除/分页设置-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination 
          layout="prev, pager, next" 
          :current-page="query.page"
          @current-change="handleCurrentChange" 
          :page-size="query.pageSize" 
          :total="query.total" 
          style="float:right;">
			</el-pagination>
		</el-col>


		<!--新增/修改界面-->
		<el-dialog title="新增/修改" :visible.sync="dictionaryFormVisible" :close-on-click-modal="false">
			<el-form :model="addDictionary" label-width="80px" :rules="dictionaryFormRules" ref="addDictionary">
				<el-form-item label="标识" prop="sn">
					<el-input v-model="addDictionary.sn" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="title" prop="title">
          <el-input  v-model="addDictionary.title" auto-complete="off"></el-input>
        </el-form-item>
				<el-form-item label="简介">
					<el-input v-model="addDictionary.intro" type="textarea"></el-input>
				</el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="addDictionary.status">
            <el-radio class="radio" :label="1">正常</el-radio>
            <el-radio class="radio" :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>



			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="dictionaryFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="save" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	export default {
		data() {
			return {
			query:{
				total: 0,
				page: 1,
        keyword: '',
        pageSize:10

      },
				dictionarys: [],
				listLoading: false,
				sels: [],//列表选中列

				
				dictionaryFormVisible: false,//新增界面是否显示
				addLoading: false,
				dictionaryFormRules: {
					sn: [
						{ required: true, message: '请输入sn', trigger: 'blur' }
					],
          title: [
						{ required: true, message: '请输入title', trigger: 'blur' }
					]
				},
				//新增/修改界面数据
				addDictionary: {
				  id:null,
          sn:"",
          title:'',
          intro:'',
          status:1
				}

			}
		},
		methods: {
      //获取当前行数据
      handleCurrentChange(val) {
				this.query.page = val;
				this.getDictionarys();
			},
			//获取数据字典列表
			getDictionarys() {
				this.listLoading = true;
				//NProgress.start();
			this.$http.post("/dictionary",this.query)
                .then((res) => {
                  res = res.data;
                  console.log(res);
                  if(res.success){
                    this.dictionarys = res.result.datas;
                    this.query.total = res.result.total;
                  }
					this.listLoading = false;
				});
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
          // debugger
          this.$http.delete("/dictionary/"+row.id)
                    .then((res) => {
						        this.listLoading = false;
                      if(res.data.success){
						            this.$message({ message: '删除成功', type: 'success' });
                      //删除以后重新从第一页开始查询
                        this.query.page = 1;
						            this.getDictionarys();
                      }else{
                        this.$message({ message: '删除失败'+res.data.message, type: 'error' });
                      }
					});
				}).catch(() => {
          this.$message({ message: '已取消删除', type: 'info' });
				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
        this.dictionaryFormVisible = true;
				this.addDictionary = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd: function () {
				this.dictionaryFormVisible = true;
				this.addDictionary = {
          id:null,
          sn:"",
          title:'',
          intro:'',
          status:1
				};
			},
			//新增/修改保存
			save: function () {
        debugger
				this.$refs.addDictionary.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							let para = Object.assign({}, this.addDictionary);

						this.$http.put("/dictionary",para)
                      .then((res) => {
								      this.addLoading = false;
                        if(res.data.success){
                          this.$message({ message: '保存成功', type: 'success' });
                          this.dictionaryFormVisible = false;
                          this.query.page = 1;
                          this.getDictionarys();
                        }else{
                          this.$message({ message: '保存失败'+res.data.message, type: 'success' });
                        }
							});
						}).catch(() => {
              this.$message({ message: '已取消新增操作', type: 'info' });
            });
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					this.$http.patch("/dictionary",ids)
                    .then((res) => {
						        this.listLoading = false;
						        if(res.data.success){
                    this.$message({ message: '删除成功', type: 'success' });

                    }else{
                    this.$message({ message: '删除失败'+res.data.message, type: 'error' });

                    }
                    this.query.page = 1;
                    this.getDictionarys();
					});
				}).catch(() => {
          this.$message({ message: '已取消批量删除', type: 'info' });
				});
			}
		},
		mounted() {
			this.getDictionarys();
		}
	}

</script>

<style scoped>

</style>